package com.harmontronics.app.pedestal.bridge;

import android.webkit.JavascriptInterface;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.blankj.utilcode.util.LogUtils;
import com.google.gson.JsonObject;
import com.harmontronics.app.pedestal.agent.WebActivity;
import com.harmontronics.app.pedestal.bridge.model.BaseBridge;

import org.json.JSONObject;

import wendu.dsbridge.CompletionHandler;

public class BMapBridge extends BaseBridge {

    private WebActivity activity;
    private LocationClient locationClient;
    private JsonObject result;

    public BMapBridge(WebActivity activity) {
        this.activity = activity;
    }

    @JavascriptInterface
    public void init(Object argsJson, CompletionHandler<String> handler) {
        LogUtils.d("BMAP.init");
        if (null == argsJson) {
            handler.complete(fail("参数不能为空", null));
            return;
        }

        try {
            JSONObject args = (JSONObject) argsJson;
            LogUtils.d("args: " + args.toString());
            if (!args.has("ak")) {
                throw new IllegalArgumentException("AK参数不能为空");
            }
            String ak = args.getString("ak");
            LocationClient.setKey(ak);
            handler.complete(success("BMAP.init success"));
        } catch (Exception e) {
            handler.complete(fail(e.getMessage(), e));
        }
    }

    private BDLocationListener listener;

    @JavascriptInterface
    public void start(Object argsJson, CompletionHandler<String> handler) {
        LogUtils.d("BMAP.start");
        if (null != argsJson) {
            try {
                JSONObject args = (JSONObject) argsJson;
                LogUtils.d("args: " + args.toString());
                if (args.has("ak")) {
                    String ak = args.getString("ak");
                    LocationClient.setKey(ak);
                }
            } catch (Exception e) {
                LogUtils.e(e);
            }
        }

        try {
            if (null == locationClient) {
                locationClient = new LocationClient(activity);
            }

            LocationClientOption option = new LocationClientOption();
            option.setCoorType("bd09ll");
            option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
            option.setIsNeedAddress(true);
            option.setOpenGnss(true);
            locationClient.setLocOption(option);

            if (null == listener) {
                listener = new BDLocationListener() {
                    @Override
                    public void onReceiveLocation(BDLocation location) {
                        String address = location.getAddrStr();    //获取详细地址信息
                        String country = location.getCountry();    //获取国家
                        String province = location.getProvince();    //获取省份
                        String city = location.getCity();    //获取城市
                        String district = location.getDistrict();    //获取区县
                        String street = location.getStreet();    //获取街道信息
                        String town = location.getTown();    //获取乡镇信息

                        double latitude = location.getLatitude();    //获取纬度信息
                        double longitude = location.getLongitude();    //获取经度信息

                        JsonObject result = new JsonObject();
                        result.addProperty("latitude", String.valueOf(latitude));
                        result.addProperty("longitude", String.valueOf(longitude));

                        result.addProperty("address", address);
                        result.addProperty("country", country);
                        result.addProperty("province", province);
                        result.addProperty("city", city);
                        result.addProperty("district", district);
                        result.addProperty("street", street);
                        result.addProperty("town", town);

                        locationClient.stop();
                        handler.complete(success(result));
                    }
                };
                locationClient.registerLocationListener(listener);
            }
            locationClient.start();
        } catch (Exception e) {
            handler.complete(fail(e.getMessage(), e));
        }
    }

    @JavascriptInterface
    public void stop(Object argsJson, CompletionHandler<String> handler) {
        LogUtils.d("BMAP.stop");
        if (null != locationClient && null != listener) {
            try {
                locationClient.unRegisterLocationListener(listener);
                locationClient.stop();
                locationClient = null;
                listener = null;
                handler.complete(success("BMAP.stop success"));
            } catch (Exception e) {
                handler.complete(fail(e.getMessage(), e));
            }
        }
    }
}
